package com.gxa.service.employee.impl;

import com.gxa.common.R.ResponseData;
import com.gxa.mapper.employee.QuanBuEmployeeListMapper;
import com.gxa.pojo.Employee;
import com.gxa.service.employee.EmployeeService;
import com.gxa.utils.EmployeeAge;
import com.gxa.utils.EmployeeGrade;
import com.gxa.utils.WorkAge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired(required = false)
    private QuanBuEmployeeListMapper quanBuEmployeeListMapper;
    /**
     * 分页+模糊查询全部员工信息
     *
     * @param currentPage   当前页数
     * @param pageSize      每页显示条数
     * @param findKeyword   姓名关键字，如果无则为‘’
     * @return              返回员工信息总条数
     */
    @Override
    public ResponseData selectAllEmployeeList(String currentPage, String pageSize, String findKeyword) {
        ResponseData responseData = new ResponseData();
        /*
        分页操作
            1. 我们需要知道总计有多少条记录(整个表中)     count代表总计
            2. 我们需要知道每页显示多少条记录(页面上)     showCount代表每页显示
            3. 然后根据固定的算法，计算出总计有多少页     page
            page = (count - 1)/showCount + 1;

            select * from user limit 0, 4;         // 这里的4代表每页显示多少条
            select * from user limit 4, 4;
            // 每页开始的下标计算：  (页码 - 1)*每页显示多少条
         */
        int currentPage_int = Integer.parseInt(currentPage); //当前页
        int pageSize_int = Integer.parseInt(pageSize);       //每页显示条数
        int count = selectCount(findKeyword);                //员工信息总条数
        int allPage = (count - 1)/pageSize_int + 1;          //总的页数
        int startIndex = (currentPage_int - 1) * pageSize_int;   //每页从第几条开始查询
        List<Employee> employeeList = quanBuEmployeeListMapper.selectEmployeeForPageAndEname(findKeyword, startIndex, pageSize_int);
        for (Employee employee: employeeList) {
            Date birthday = employee.getBirthday();           //获取员工出生日期
            Date entryDate = employee.getEntryDate();         //获取员工入职日期

            int age = EmployeeAge.getEmployeeAge(birthday);   //EmployeeAge计算员工年龄工具类计算员工年龄
            int gongLin = WorkAge.getWorkAge(entryDate);      //WorkAge计算员工工龄工具类调用getWorkAge方法计算员工工龄
            String grade = EmployeeGrade.getGrade(gongLin);   //EmployeeGrade工具类通过员工gongLin计算员工级别
            employee.setAge(age);
            employee.setGongLin(gongLin);
            employee.setGrade(grade);
        }
        responseData.setCode(200);
        responseData.setMsg("查询全部员工数据成功");
        responseData.setTotal(count);
        responseData.setData(employeeList);
        return responseData;
    }
    /**
     * 员工信息总条数
     * @param findKeyword 模糊查询员工姓名关键字
     * @return            返回员工信息总条数
     */
    @Override
    public int selectCount(String findKeyword) {
        Integer count = quanBuEmployeeListMapper.selectAllCount(findKeyword);
        return count;
    }

    /**
     * //将在职员工设置为离职员工
     * @param employee        员工信息实体类
     * @return                返回给客户端实体类
     */
    @Override
    public ResponseData setDepartEmployee(Employee employee) {
        ResponseData responseData = new ResponseData();
        quanBuEmployeeListMapper.setDepartEmployee(employee);
        responseData.setCode(200);
        responseData.setMsg("成功将员工设置为离职状态");
        return responseData;
    }

    /**
     * 编辑员工信息
     * @param employee        员工信息实体类
     * @return                返回给客户端实体类
     */
    @Override
    public ResponseData updateEmployee(Employee employee) {
        ResponseData responseData = new ResponseData();
        quanBuEmployeeListMapper.updateEmployee(employee);
        responseData.setCode(200);
        responseData.setMsg("员工信息编辑成功");
        return responseData;
    }

    /**
     * 通过员工编号ID删除员工信息
     * @param idList         员工编号ID数组
     * @return           返回给客户端实体类
     */
    @Override
    public ResponseData delEmployee(List<Integer> idList) {
        ResponseData responseData = new ResponseData();
        quanBuEmployeeListMapper.delEmployee(idList);
        responseData.setCode(200);
        responseData.setMsg("成功删除员工信息");
        return responseData;
    }



}
